
package com.netease.xone.fragment;

import java.util.Timer;
import java.util.TimerTask;

import protocol.XoneCallBack;
import protocol.XoneService;
import Utils.XoneUtil;
import android.app.Service;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;

import com.netease.pkgRelated.XoneConsts.BUNDLE_KEY;
import com.netease.pkgRelated.XoneConsts.INPUT_CELLPHONE_TYPE;
import com.netease.xone.R;
import com.netease.xone.app.XoneApp;
import com.netease.xone.widget.EditTextWithClear;

public class FragCellPhoneInputNum extends FragCellPhoneBase {

    private int mAction;
    private EditText mPhoneNumber;
    private Button mButtonNext;
    private FragmentManager mFragmentManager;

    private int mTitleResId;

    public static FragCellPhoneInputNum newInstance(int input_num_type) {
        FragCellPhoneInputNum fragment = new FragCellPhoneInputNum();
        Bundle b = new Bundle();
        b.putInt(BUNDLE_KEY.CELLPHONE_TYPE, input_num_type);
        fragment.setArguments(b);
        return fragment;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        setHasOptionsMenu(true);
        Bundle bundle = getArguments();
        if (bundle != null && bundle.containsKey(BUNDLE_KEY.CELLPHONE_TYPE)) {
            mAction = bundle.getInt(BUNDLE_KEY.CELLPHONE_TYPE);
        }
        if (mAction == INPUT_CELLPHONE_TYPE.Register) {
            mTitleResId = R.string.cellphoneRegister;
        } else {
            mTitleResId = R.string.findPassword;
        }
        getActivity().setTitle(mTitleResId);
        mFragmentManager = getFragmentManager();
        View view = inflater.inflate(R.layout.fragment_input_phone_number, container, false);
        mPhoneNumber = (EditTextWithClear)view.findViewById(R.id.inputPhoneNumInput);
        
        mButtonNext = (Button)view.findViewById(R.id.cellphoneNextBtn);
        mButtonNext.setOnClickListener(new OnClickListener() {
            
            @Override
            public void onClick(View v) {
                doNext();
            }
        });
        return view;
    }

    @Override
    public void onStart() {
        super.onStart();
        new Timer().schedule(new TimerTask() {

            @Override
            public void run() {
                if (getActivity() != null) {
                    InputMethodManager imm = (InputMethodManager)XoneApp.getAppInstance()
                            .getSystemService(Service.INPUT_METHOD_SERVICE);
                    imm.showSoftInput(mPhoneNumber, 0);
                }
            }
        }, 300);
        mPhoneNumber.requestFocus();
    }

//    @Override
//    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
//        menu.add(0, MENU_ID.menu_cellphone_next1, 0, getActivity().getText(R.string.nextStep))
//                .setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
//        super.onCreateOptionsMenu(menu, inflater);
//    }
//
//    @Override
//    public boolean onOptionsItemSelected(MenuItem item) {
//        if (item.getItemId() == MENU_ID.menu_cellphone_next1) {
//            doNext();
//        }
//
//        return super.onOptionsItemSelected(item);
//    }
    private void doNext(){
        String phoneNum = mPhoneNumber.getText().toString();
        int msgid = -1;
        if (TextUtils.isEmpty(phoneNum)) {
            msgid = R.string.inputPhoneNumberHint;
        } else if (!XoneUtil.isValidatePhoneNum(phoneNum)) {
            msgid = R.string.phoneNumInvalid;
        }
        if (msgid >= 0) {
            showToast(msgid);

        } else {
            Fragment frag = FragCellPhoneInputCaptcha.newInstance(mAction, phoneNum);
            mFragmentManager.beginTransaction()
                    .replace(R.id.activity_CellPhone_container_id, frag).addToBackStack(null)
                    .commit();
        }
    }
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        XoneService.getInstance().addListener(mXoneCallBack);
    }
    @Override
    public void onDestroy() {
        super.onDestroy();
        XoneService.getInstance().removeListener(mXoneCallBack);
    }
    @Override
    protected void onLeftTopBack() {
        getFragmentManager().popBackStack();
    }
    private XoneCallBack mXoneCallBack = new XoneCallBack() {
        @Override
        public void onModifyCellPhonePw(int transactionId) {
            getFragmentManager().popBackStack();
        };
    };
}
